package cn.iocoder.yudao.module.wine.job;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.wine.dal.dataobject.countnameconfig.CountNameConfigDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplycount.SupplyCountDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplymarket.SupplyMarketDO;
import cn.iocoder.yudao.module.wine.dal.mysql.countnameconfig.CountNameConfigMapper;
import cn.iocoder.yudao.module.wine.dal.mysql.supplycount.SupplyCountMapper;
import cn.iocoder.yudao.module.wine.service.supplymarket.SupplyMarketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zyc
 * @createDate 2024/7/23 09:53
 */
@Component
@Slf4j
public class SupplyCountJob implements JobHandler {

    @Resource
    private SupplyCountMapper supplyCountMapper;

    @Resource
    private SupplyMarketService supplyMarketService;


    @Resource
    private CountNameConfigMapper countNameConfigMapper;

    @Override
    @TenantIgnore
    public String execute(String param) throws Exception {
        log.info("日k定时任务开始");
        List<CountNameConfigDO> countNameConfigDOS = countNameConfigMapper.selectList();
        log.info("日k定时任务配置数据:{}", countNameConfigDOS);
        if (!CollectionUtil.isEmpty(countNameConfigDOS)){
            countNameConfigDOS.forEach(item -> {
                List<SupplyMarketDO> list = supplyMarketService.getSupplyMarketByDay(item);
                log.info("日k定时任务供求信息:{}", list);
                log.info("日k定时任务看看时间:{}", countNameConfigDOS.get(0).getCreateTime());
                if (!CollectionUtil.isEmpty(list)){
                    //最大值
                    SupplyMarketDO max = list.stream()
                            .max((o1, o2) -> o1.getAveragePrice().compareTo(o2.getAveragePrice()))
                            .orElse(null);
                    //最小值
                    SupplyMarketDO min = list.stream()
                            .min((o1, o2) -> o1.getAveragePrice().compareTo(o2.getAveragePrice()))
                            .orElse(null);
                    SupplyCountDO supplyCountDO = new SupplyCountDO();
                    supplyCountDO.setInfoTitle(item.getCountName());
                    supplyCountDO.setLastPrice(list.get(0).getAveragePrice());
                    supplyCountDO.setFirstPrice(list.get(list.size() - 1).getAveragePrice());
                    if (max != null){
                        supplyCountDO.setMaxPrice(max.getAveragePrice());
                    }
                    if (min != null){
                        supplyCountDO.setMinPrice(min.getAveragePrice());
                    }

                    supplyCountDO.setConfigId(item.getId());
                    supplyCountDO.setCountTime(LocalDateTime.now());
                    supplyCountMapper.insert(supplyCountDO);
                }

            });


        }
        log.info("日k定时任务结束");
        return "日k定时任务执行完成！";
    }
}
